package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.patrol.PatrolLocation;
import com.motu.monstercity.server.game.commondata.patrol.PatrolTable;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class PatrolBaseData {
    private static Logger logger = LoggerFactory.getLogger(PatrolBaseData.class);
    private static ConcurrentHashMap<Integer, PatrolTable> patrolTableMap = new ConcurrentHashMap<>(); // 秘书巡查表
    private static ConcurrentHashMap<Integer, PatrolTable> patrolTableScretaryMap = new ConcurrentHashMap<>(); // 秘书巡查表（按秘书ID存储）

    public static void initBaseData(List<Future> futures) {
        futures.add(initPatrolTable());
        futures.add(initPatrolLocation());
    }

    /**
     * 初始化秘书巡查表
     */
    public static Future<Void> initPatrolTable() {
        String table = "patrol_table";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, PatrolTable> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, PatrolTable> secretaryTmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        PatrolTable entity = new PatrolTable(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        secretaryTmp.put(entity.getSecretaryId(), entity);
                    }
                    patrolTableMap = tmp;
                    patrolTableScretaryMap = secretaryTmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static PatrolTable getPatrolTable(int id) {
        PatrolTable result = patrolTableMap.get(id);
        if (result == null) {
            logger.error("getPatrolTable error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, PatrolTable> getPatrolTableMap() { return patrolTableMap; }
    public static ConcurrentHashMap<Integer, PatrolTable> getPatrolTableScretaryMap() { return patrolTableScretaryMap; }
    public static PatrolTable getPatrolTableBySecretaryId(int secretaryId) {
        PatrolTable result = patrolTableScretaryMap.get(secretaryId);
        if (result == null) {
            logger.error("getPatrolTableBySecretaryId error key = " + secretaryId);
        }
        return result;
    }
    private static ConcurrentHashMap<Integer, PatrolLocation> patrolLocationMap = new ConcurrentHashMap<>(); // 秘书巡查事件表

    /**
     * 初始化秘书巡查事件表
     */
    public static Future<Void> initPatrolLocation() {
        String table = "patrol_location";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, PatrolLocation> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        PatrolLocation entity = new PatrolLocation(result.getJsonObject(i));
                        tmp.put(entity.getEventId(), entity);
                    }
                    patrolLocationMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static PatrolLocation getPatrolLocation(int id) {
        PatrolLocation result = patrolLocationMap.get(id);
        if (result == null) {
            logger.error("getPatrolLocation error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, PatrolLocation> getPatrolLocationMap() { return patrolLocationMap; }
}
